package com.itheima.job;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

//定时处理垃圾图片
//定时任务组件Quartz
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        //根据Redis中保存的两个set集合差值计算，获得垃圾图片名称集合
        Set<String> stringSet = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (stringSet!=null){
            for (String s : stringSet) {
                //删除七牛云中垃圾的图片
                QiniuUtils.deleteFileFromQiniu(s);
                //删除redis多余的垃圾图片
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);

                System.out.println("删除正在执行");
            }
        }
    }
}
